var selectedRow = '';
var def_editType = 'add';

$(function () {
    regValidate(["#_dictDataForm"]);
    window.actionEvents = {
        'click .infoView': function(e, value, row, index) {
            //编辑操作
            selectedRow = row;
            infoEnumPage();
        },
    }
});

$('#editEnumPage').on('hidden.bs.modal', function () {
    // 执行一些动作...
    $('#_dictCode').removeAttrs('disabled');
    resetFormTip('#_dictDataForm');
    resetInputCss('#_dictDataForm');
});

function doRefresh() {
    selectedRow = '';
    var dictName = $.trim($('#dictName').val());
    $('#dataTable').bootstrapTable('refresh', {
        url: basePath + 'sysDictionary/pageFindDictData?t=' + new Date().getTime(),
        query: {
            dictName : dictName,
            groupCode : p_groupCode,
        },
    });
}

function getParams(params) {
    var dicName = $.trim($('#dicName').val());
    return {
        dicName : dicName,
        groupCode : p_groupCode,
        pageSize : params.pageSize,
        pageNo : params.pageNumber
    };
}

function openAddEnumPage() {
    def_editType = 'add';
    $('#_modelTitle').html('新增枚举');
    $('#_groupName').val(p_groupName);
    $('#_groupName').attr('disabled', 'disabled');
    $('#_groupCode').val(p_groupCode);
    $('#_groupCode').attr('disabled', 'disabled');
    $('#_dictName').val('');
    $('#_dictCode').val('');
    $('#_sortNum').val('');
    $('#_extValue').val('');
    $('#remark').val('');
    $('#editEnumPage').modal('show');
}

function openEditEnumPage() {
    def_editType = 'edit';
    if (!selectedRow.id || selectedRow.id == '') {
        toastr.error(common_no_choose_msg.format("编辑"));
        return;
    }
    $('#_modelTitle').html('编辑枚举');
    $('#_groupName').val(selectedRow.groupName);
    $('#_groupName').attr('disabled', 'disabled');
    $('#_groupCode').val(selectedRow.groupCode);
    $('#_groupCode').attr('disabled', 'disabled');
    $('#_dictName').val(selectedRow.dictName);
    $('#_dictCode').val(selectedRow.dictCode);
    $('#_dictCode').attr('disabled', 'disabled');
    $('#_sortNum').val(selectedRow.sortNum);
    $('#_extValue').val(selectedRow.extValue);
    $('#remark').val(selectedRow.remark);
    $('#editEnumPage').modal('show');
}

function closeEditEnumPage() {
    $('#editEnumPage').modal('hide');
    $('#_saveBtn').show();
    $('#_groupName').removeAttrs('disabled');
    $('#_groupCode').removeAttrs('disabled');
    $('#_dictCode').removeAttrs('disabled');
    resetFormTip('#_dictDataForm');
    resetInputCss('#_dictDataForm');
}

function closeEnumInfoPage() {
    $('#enumInfoPage').modal('hide');
    resetFormTip('#_dictDataForm1');
}

function doSaveEnum() {
    if(!$("#_dictDataForm").valid()) return;//校验不通过
    var groupCode = $.trim($('#_groupCode').val());
    var groupName = $.trim($('#_groupName').val());
    var dictName = $.trim($('#_dictName').val());
    var dictCode = $.trim($('#_dictCode').val());
    var sortNum = $.trim($('#_sortNum').val());
    var extValue = $.trim($('#_extValue').val());
    var remark = $.trim($('#remark').val());
    if (!sortNum || sortNum == '') {
        sortNum = 999;
    }
    var postData = {
        groupCode : groupCode,
        groupName : groupName,
        dictName : dictName,
        dictCode : dictCode,
        extValue : extValue,
        remark : remark,
        sortNum : sortNum,
    }
    var url = basePath + 'sysDictionary/addDict';
    if (def_editType == 'edit') {
        url = basePath + 'sysDictionary/editDict';
        postData.id = selectedRow.id;
    }
    $.ajax({
        type: 'post',
        url: url,
        dataType: 'json',
        contentType: "application/json",
        data: JSON.stringify(postData),
        success : function (data) {
            if(data && data.isSuccess){
                toastr.success(common_save_success);
                closeEditEnumPage();
                doRefresh();
            }else if(data&&!data.isSuccess){
                toastr.error(data.errorDesc);
            }else{
                toastr.error(common_save_fail);
            }
        },
        error: function (request) {
            toastr.error(common_system_error);
        }
    });
}

function deleteEnum() {
    if (!selectedRow.id) {
        toastr.error(common_no_choose_msg.format("删除"));
        return;
    }
    var strHtml = '是否删除枚举['+selectedRow.dictName+']？该操作会变更枚举数据！';
    swalToastr('', strHtml, 'warning', doEnumDel);
}

function doEnumDel() {
    var postData = {
        id : selectedRow.id,
        groupCode : selectedRow.groupCode,
        dictCode : selectedRow.dictCode,
        status : 'delete'
    };
    $.ajax({
        type: 'post',
        url: basePath + 'sysDictionary/deleteDict',
        dataType: 'json',
        contentType: "application/json",
        data: JSON.stringify(postData),
        success : function (data) {
            if(data && data.isSuccess){
                toastr.success('删除成功');
                doRefresh();
            }else if(data&&!data.isSuccess){
                toastr.error(data.errorDesc);
            }else{
                toastr.error('删除失败');
            }
        },
        error: function (request) {
            toastr.error(common_system_error);
        }
    });
}

function infoEnumPage() {
    if (!selectedRow.id || selectedRow.id == '') {
        toastr.error(common_no_choose_msg.format("操作"));
        return;
    }
    $('#lab_groupName').html(selectedRow.groupName);
    $('#lab_groupCode').html(selectedRow.groupCode);
    $('#lab_dictCode').html(selectedRow.dictCode);
    $('#lab_dictName').html(selectedRow.dictName);
    $('#lab_sortNum').html(selectedRow.sortNum);
    $('#lab_extValue').html(selectedRow.extValue);
    $('#lab_remark').html(selectedRow.remark);
    $('#enumInfoPage').modal('show');
}

function doBack() {
    window.location.href = basePath + 'sysDictionary/dictPage?t=' + new Date().getTime() +
        '&dictName=' + searchDictInfo.dictName + '&pageNo=' + searchDictInfo.pageNo + '&pageSize=' + searchDictInfo.pageSize;
}

//左边选中事件
$("#dataTable").on("click-row.bs.table",function(e,row,ele){
    $(".success").removeClass("success");
    $(ele).addClass("success");
    selectedRow = row;
});